<template>
	<div class="integral_set">
		<use-box :loading="formLoading">
			<el-form :model="form" :rules="rules" ref="ruleForm">
				<h3>积分参数</h3>
				<el-form-item>
					<div class="dflex margin_t_20">
						<div class="front_txt">注册会员可获</div>
						<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
							v-model.number="form.register_gain_integral" type="number"
							oninput="value = parseInt(value) || 1"></el-input>
						<div class="rear_txt">积分。</div>
					</div>
				</el-form-item>
				<el-form-item>
					<div class="dflex">
						<div class="front_txt">会员每天可分享</div>
						<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
							v-model.number="form.everyday_share_cnt" type="number"
							oninput="value = parseInt(value) || 1"></el-input>
						<div class="rear_txt">次，分享一次获</div>
						<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
							v-model.number="form.share_once_gain_integral" type="number"
							oninput="value = parseInt(value) || 1"></el-input>
						<div class="rear_txt">积分。</div>
					</div>
				</el-form-item>
				<el-form-item>
					<div class="dflex">
						<div class="front_txt">每天签到可获</div>
						<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
							v-model.number="form.everyday_sign_in_gain_integral" type="number"
							oninput="value = parseInt(value) || 1"></el-input>
						<div class="rear_txt">积分。</div>
					</div>
				</el-form-item>
				<el-form-item>
					<div class="dflex">
						<div class="front_txt">连续签到满</div>
						<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
							v-model.number="form.continuous_sign_in_day" type="number"
							oninput="value = parseInt(value) || 1"></el-input>
						<div class="rear_txt">天，额外赠送</div>
						<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
							v-model.number="form.extra_gift_integral" type="number"
							oninput="value = parseInt(value) || 1"></el-input>
						<div class="rear_txt">积分。</div>
					</div>
				</el-form-item>
				<el-form-item>
					<div class="dflex_vertical_c">
						<div class="front_txt">下单返积分比例</div>
						<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
							v-model.number="form.return_integral_ratio" type="number"
							oninput="value = parseInt(value) || 1"></el-input>
						<div class="rear_txt">%。</div>
						<div class="remark_txt margin_t_0 h_initial">1元可获{{ form.return_integral_ratio }}积分</div>
					</div>
				</el-form-item>
				<el-form-item prop="once_most_spendable_integral" ref="once_most_spendable_integral">
					<div class="dflex_vertical_c">
						<div class="front_txt">下单时</div>
						<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
							v-model.number="form.one_money_deduction" type="number"
							oninput="value = parseInt(value) || 1"></el-input>
						<div class="rear_txt">积分 = 1元，一次最多可使用</div>
						<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
							v-model.number="form.once_most_spendable_integral" type="number"
							oninput="value = parseInt(value) || 1"></el-input>
						<div class="rear_txt">积分。</div>
					</div>
				</el-form-item>
				<el-form-item>
					<div class="dflex_vertical_c">
						<div class="front_txt margin_r_10">年底积分清零</div>
						<el-switch :disabled="addBtnLoading" v-model="form.yearly_integral_reset" active-color="#ff6a6c"
							inactive-color="#bbb" active-value="是" inactive-value="否"></el-switch>
					</div>
				</el-form-item>
				<h3>积分说明</h3>
				<el-form-item label-width="120px">
					<use-editor :disabled="addBtnLoading" class="margin_t_20" ref="editor" @editorChange="editorChange">
					</use-editor>
				</el-form-item>
				<el-form-item label-width="210px">
					<use-affix position="bottom" :offset="58">
						<el-button :loading="addBtnLoading" round icon="el-icon-check" class="confirm-btn"
							@click="submitForm('ruleForm')" :disabled="!stopSta">
							提交
						</el-button>
					</use-affix>
				</el-form-item>
			</el-form>
		</use-box>
	</div>
</template>

<script>
	const __name = 'wmzmall-integral-parameter';
	export default {
		data() {
			const validate = async (rule, value, callback) => {
				if (value < this.form.one_money_deduction) {
					this.stopSta = false;
					return callback(new Error('最多可使用积分不能小于，下单时抵现积分'));
				} else {
					this.stopSta = true;
				}
				callback();
			};
			return {
				dataId: '',
				formLoading: false,
				form: {
					register_gain_integral: 0,
					everyday_share_cnt: 0,
					share_once_gain_integral: 0,
					everyday_sign_in_gain_integral: 0,
					continuous_sign_in_day: 0,
					extra_gift_integral: 0,
					return_integral_ratio: 0,
					one_money_deduction: 0,
					once_most_spendable_integral: 0,
					yearly_integral_reset: '否',
					integral_desc: ''
				},
				addBtnLoading: false,
				rules: {
					once_most_spendable_integral: [{
						required: true,
						validator: validate,
						trigger: 'change'
					}]
				},
				stopSta: true
			};
		},
		methods: {
			async loadData() {
				this.formLoading = true;
				await this.$db[__name].tofirst({
					loading: false
				}).then(res => {
					if (res.code == 200) {
						res.datas = res.datas || {};

						this.dataId = res.datas._id || '';

						for (let item in this.form) {
							this.form[item] = res.datas[item];
						}

						if (res.datas && res.datas.integral_desc) {
							this.form.integral_desc = res.datas.integral_desc;
							this.$refs.editor.setContent(this.form.integral_desc);
						}
					}
					this.formLoading = false;
				});
			},
			submitForm(formName) {
				this.addBtnLoading = true;
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						if (!this.dataId) {
							this.$db[__name].add(this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.addBtnLoading = false;
									return;
								}
								this.loadData();
							}).catch(() => {
								this.addBtnLoading = false;
							})
						} else {
							this.$db[__name].update(this.dataId, this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.addBtnLoading = false;
									return;
								}
								this.loadData();
							}).catch(() => {
								this.addBtnLoading = false;
							})
						}
					}
				});
			},
			editorChange(res) {
				this.form.integral_desc = res;
			}
		},
		mounted() {
			this.$nextTick(() => {
				this.$api.scrollOnStorage('.use-box', this.$route.path);
			});

			this.$nextTick(() => {
				this.$api.scrollToStorage('.use-box', this.$route.path);
			});
		},
		created() {
			this.loadData();
		}
	};
</script>

<style>
	>>>.input {
		width: 100px;
		margin: 0 10px;
	}

	>>>.input .el-input__inner {
		width: 100%;
		background-color: #f6f6f6;
		border-color: #f6f6f6;
		padding: 0 0 0 15px;
	}

	.front_txt {
		width: 200px;
		text-align: right;
		font-size: 13px;
		user-select: none;
	}

	.rear_txt {
		font-size: 13px;
		display: flex;
		align-items: center;
		user-select: none;
	}

	.integral_set>>>.el-form-item__error {
		margin-left: 210px;
	}
</style>
